// 动态拼接钉钉授权页链接
const corpId = import.meta.env.VITE_CORP_ID;
const clientId = import.meta.env.VITE_CLIENT_ID;
const authBtn = document.getElementById('open-dingtalk-auth');
if (authBtn) {
    authBtn.onclick = function() {
        const url = `https://login.dingtalk.com/oauth2/auth?redirect_uri=http://localhost:5173/user.html&response_type=code&client_id=${clientId}&scope=openid%20corpid&prompt=consent`;
        window.open(url, '_blank');
    };
} 
// 钉钉扫码登录二维码初始化
function initDingLogin() {
    if (window.DTFrameLogin) {
        const corpId = import.meta.env.VITE_CORP_ID ? import.meta.env.VITE_CORP_ID : 'ding0b79433bb95f7fc64ac5d6980864d335';
        const clientId = import.meta.env.VITE_CLIENT_ID ? import.meta.env.VITE_CLIENT_ID : 'dinglfjrisylxmm2anpz';
        window.DTFrameLogin(
            {
                id: 'self_defined_element',
                width: 300,
                height: 300,
            },
            {
                // 注意：redirect_uri 需为完整URL，扫码后钉钉会带code跳转到这里
                redirect_uri: encodeURIComponent('http://localhost:5173/user.html'),
                client_id: clientId,
                scope: 'openid corpid',
                response_type: 'code',
                state: '1',
                prompt: 'consent',
                corpId: corpId,
            },
            (loginResult) => {
                const {redirectUrl, authCode, state} = loginResult;
                // 这里可以直接进行重定向
                window.location.href = redirectUrl;
                console.log(authCode);
            },
            (errorMsg) => {
                // 这里一般需要展示登录失败的具体原因
                alert(`Login Error: ${errorMsg}`);
            },
        );
    } else {
        setTimeout(initDingLogin, 100);
    }
}
initDingLogin(); 